package com.lagou.lagouservicecode.service;

import com.lagou.api.CodeApi;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

/**
 * @Authther: mrwu
 * @date:2021/4/21-04
 * @Description:com.lagou.lagouservicecode.service
 */
@DubboService//暴露dabbo服务
public class CodeApiService implements CodeApi {
    static final String CODE_PRI_FIX ="mailcode:";

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    /**
     * @param email
     * @param code
     * @return 验证码是否有效
     */

    @Override
    public boolean isvalidate(String code, String email) {
        //从redis中取出数据和code进行对比
        ValueOperations opsForValue = redisTemplate.opsForValue();//字符串操作连接
        String result =(String) opsForValue.get(CODE_PRI_FIX + email);
        System.out.println("redis获取的key==="+CODE_PRI_FIX + email+"值为"+result);
        if (result==null){
            return false;
        }
        return result.equals(code);
    }
}
